package flc.ast.activity;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumChooseAdapter;
import flc.ast.adapter.PictureFilterAdapter;
import flc.ast.databinding.ActivityAlbumChooseBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.constant.Extra;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class AlbumChooseActivity extends BaseAc<ActivityAlbumChooseBinding> {

    public static int type;//0图标替换 1图片处理
    private AlbumChooseAdapter mAlbumAdapter;
    private String mChoosePath;
    private int mIndex;
    private List<SelectMediaEntity> listShow = new ArrayList<>();
    private String add = "";
    public static final int CHOOSE_FIGURE_CODE = 230;


    @Override
    protected int onCreate() {
        return R.layout.activity_album_choose;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivAlbumChooseBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivAlbumChooseConfirm.setOnClickListener(this);
        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mAlbumAdapter = new AlbumChooseAdapter();
        mDataBinding.rvPictureList.setAdapter(mAlbumAdapter);
        mAlbumAdapter.setOnItemClickListener(this);
        //扣像添加人像
        add = getIntent().getStringExtra(Content.Add);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAlbumChooseConfirm:
                if (!TextUtils.isEmpty(mChoosePath)) {
                    if (type == 0) {
                        Intent intent = new Intent();
                        intent.putExtra(Extra.PATH, mChoosePath);
                        setResult(RESULT_OK, intent);
                        onBackPressed();
                    } else if (type == 1) {
                        if (TextUtils.isEmpty(add)) {
                            PicHandleActivity.type = 0;
                            PicHandleActivity.imgPath = mChoosePath;
                            startActivity(PicHandleActivity.class);
                        } else {
                            PicHandleActivity.type = 1;
                            PicHandleActivity.imgPath = mChoosePath;
                            startActivityForResult(new Intent(mContext, PicHandleActivity.class), CHOOSE_FIGURE_CODE);
                        }
                    } else if (type == 2) {
                        PicBeautyActivity.imgPath = mChoosePath;
                        startActivity(PicBeautyActivity.class);
                    } else if (type == 3) {
                        PicTextActivity.imgPath = mChoosePath;
                        startActivity(PicTextActivity.class);
                    } else if (type == 4) {
                        PicFilterActivity.imgPath = mChoosePath;
                        startActivity(PicFilterActivity.class);
                    } else if (type == 5) {
                        PicStickerActivity.imgPath = mChoosePath;
                        startActivity(PicStickerActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.please_choose_photo_hint);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mAlbumAdapter.getItem(mIndex).setChecked(false);
        mAlbumAdapter.getItem(position).setChecked(true);
        mIndex = position;
        mChoosePath = mAlbumAdapter.getItem(position).getPath();
        mAlbumAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPictureList.setVisibility(View.GONE);
                        mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    mAlbumAdapter.setList(listShow);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_FIGURE_CODE) {
                Log.e("TAG", "onActivityResult:222 ");
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                onBackPressed();
            }
        }
    }
}